package com.buba.system.controller;

import com.buba.system.pojo.Employee;
import com.buba.system.pojo.SystemUser;
import com.buba.system.service.AccountService;
import com.mysql.cj.api.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 账号
 */
@RestController
@RequestMapping("/Account")
public class AccountController {
    private AccountService accountService;

    @RequestMapping("/changePassword")
    public Map<String,Object> changePassword(String oldPassword, String newPassword, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        //尽量避免对数据库进行操作
        //从Session中拿去两个东西 当前登陆账号id， 当前登录的账号密码
        Employee employee = (Employee)session.getAttribute("employee");
        SystemUser systemUser = employee.getSystemUser();
        String password = employee.getSystemUser().getPassword();

        //字符串比较一定要使用equals（）
        //防止空指针异常
        if (!oldPassword.equals(password)){
            map.put("message","旧密码输入错误");
            return map;
        }
        map.put("userId",systemUser.getUserid());
        map.put("newPassword",newPassword);
        //进行修改密码操作
        int count = accountService.changePassword(map);
        map.put("message","修改成功");
        return map;
    }


    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
}
